<?php

/* Author: tbloc@cantho.gov.vn */
/* Created Date : 17-07-2014 10:10:51 */

class M_Binh_luan {

//Properties
    private $BL_ID;
    private $HH_ID;
    private $TK_ID;
    private $BL_NOIDUNG;
    private $BL_NGAYDANG;
    private $TK_AVATAR;
    private $TK_USERNAME;
//properties set get 
    public function setBL_ID($BL_ID) {
        $this->BL_ID = $BL_ID;
    }

    public function getBL_ID() {
        return $this->BL_ID;
    }
    
    public function setTK_AVATAR($TK_AVATAR) {
        $this->TK_AVATAR = $TK_AVATAR;
    }

    public function getTK_AVATAR() {
        return $this->TK_AVATAR;
    }
    public function setTK_USERNAME($TK_USERNAME) {
        $this->TK_USERNAME = $TK_USERNAME;
    }

    public function getTK_USERNAME() {
        return $this->TK_USERNAME;
    }

    public function setHH_ID($HH_ID) {
        $this->HH_ID = $HH_ID;
    }

    public function getHH_ID() {
        return $this->HH_ID;
    }

    public function setTK_ID($TK_ID) {
        $this->TK_ID = $TK_ID;
    }

    public function getTK_ID() {
        return $this->TK_ID;
    }

    public function setBL_NOIDUNG($BL_NOIDUNG) {
        $this->BL_NOIDUNG = $BL_NOIDUNG;
    }

    public function getBL_NOIDUNG() {
        return $this->BL_NOIDUNG;
    }

    public function setBL_NGAYDANG($BL_NGAYDANG) {
        $this->BL_NGAYDANG = $BL_NGAYDANG;
    }

    public function getBL_NGAYDANG() {
        return $this->BL_NGAYDANG;
    }

//crud methods
    public function insert($db = null) {
        if ($db != null) {
            $sql = "INSERT INTO `binh_luan`(                        
			`BL_ID`, 
				`HH_ID`, 
				`TK_ID`, 
				`BL_NOIDUNG`, 
				`BL_NGAYDANG` 
		)
	VALUES(
    		:BL_ID, 
	    		:HH_ID, 
	    		:TK_ID, 
	    		:BL_NOIDUNG, 
	    		:BL_NGAYDANG 
	    );";
            $parameters = array(
                ":BL_ID",
                ":HH_ID",
                ":TK_ID",
                ":BL_NOIDUNG",
                ":BL_NGAYDANG"
            );
            $values = array(
                $this->getBL_ID(),
                $this->getHH_ID(),
                $this->getTK_ID(),
                $this->getBL_NOIDUNG(),
                $this->getBL_NGAYDANG()
            );
            $types = array(
                PDO::PARAM_INT,
                PDO::PARAM_INT,
                PDO::PARAM_INT,
                PDO::PARAM_STR,
                PDO::PARAM_STR
            );
            $lastInsertID = $db->executeNonQuery($sql, $parameters, $values, $types, true);
            $this->setBL_ID($lastInsertID);
        }
    }

    public function delete($db = null) {
        if ($db != null) {
            $sql = "update `binh_luan`
	                set `is_active`=0
	                where `BL_ID` = :BL_ID;";
            $parameters = array(":BL_ID");
            $values = array($this->getBL_ID());
            $types = array(PDO::PARAM_INT); //Kiem tra lai kieu du lieu nhe
            $result = $db->executeNonQuery($sql, $parameters, $values, $types);
            return $result;
        }
    }

    public function update($db = null) {
        if ($db != null) {
            $sql = "UPDATE `binh_luan` SET 
			`BL_ID`=:BL_ID,
				`HH_ID`=:HH_ID,
				`TK_ID`=:TK_ID,
				`BL_NOIDUNG`=:BL_NOIDUNG,
				`BL_NGAYDANG`=:BL_NGAYDANG
			WHERE `BL_ID` = :BL_ID;";
            $parameters = array(
                ":BL_ID",
                ":HH_ID",
                ":TK_ID",
                ":BL_NOIDUNG",
                ":BL_NGAYDANG"
            );
            $values = array(
                $this->getBL_ID(),
                $this->getHH_ID(),
                $this->getTK_ID(),
                $this->getBL_NOIDUNG(),
                $this->getBL_NGAYDANG()
            );
            $types = array(
                PDO::PARAM_INT,
                PDO::PARAM_INT,
                PDO::PARAM_INT,
                PDO::PARAM_STR,
                PDO::PARAM_STR
            );
            $result = $db->executeNonQuery($sql, $parameters, $values, $types);
            return $result;
        }
    }

    public function getByID($db = null) {
        if ($db != null) {
            $sql = "SELECT
			`BL_ID`,
				`HH_ID`,
				`TK_ID`,
				`BL_NOIDUNG`,
				`BL_NGAYDANG`
			FROM `binh_luan` WHERE `BL_ID` = :BL_ID and `is_active`=1;";
            $parameters = array(":BL_ID");
            $values = array($this->getBL_ID());
            $types = array(PDO::PARAM_INT); //kiem tra lai kieu du lieu nhe
            $rows = $db->executeQuery($sql, $parameters, $values, $types);
            if ($rows != null) {
                $row = $rows->fetch();
                $this->setBL_ID($row->BL_ID);
                $this->setHH_ID($row->HH_ID);
                $this->setTK_ID($row->TK_ID);
                $this->setBL_NOIDUNG($row->BL_NOIDUNG);
                $this->setBL_NGAYDANG($row->BL_NGAYDANG);
            }
            return $this;
        }
    }

    public function getList($db = null) {
        if ($db != null) {
            $sql = "SELECT
			`BL_ID`,
				`HH_ID`,
				`TK_ID`,
				`BL_NOIDUNG`,
				`BL_NGAYDANG`
			FROM `binh_luan` WHERE `is_active`=1;";
            $rows = $db->executeQuery($sql);
            $objects = array();
            if ($rows != null) {
                while ($row = $rows->fetch()) {
                    $obj = new M_Binh_luan();
                    $obj->setBL_ID($row->BL_ID);
                    $obj->setHH_ID($row->HH_ID);
                    $obj->setTK_ID($row->TK_ID);
                    $obj->setBL_NOIDUNG($row->BL_NOIDUNG);
                    $obj->setBL_NGAYDANG($row->BL_NGAYDANG);
                    $objects[] = $obj;
                }
                return $objects;
            }
        }
    }

    public function getListBinhLuan($db = null) {
        if ($db != null) {
            $sql = "SELECT
                    BL_ID,
                    HH_ID,
                    tai_khoan.TK_ID,
                    tai_khoan.TK_AVATAR,
                    tai_khoan.TK_USERNAME,
                    BL_NOIDUNG,
                    BL_NGAYDANG
                    FROM binh_luan JOIN tai_khoan
                    ON binh_luan.TK_ID=tai_khoan.TK_ID
                    WHERE HH_ID=:HH_ID";
            $parameters = array(":HH_ID");
            $values = array($this->getHH_ID());
            $types = array(PDO::PARAM_INT); //kiem tra lai kieu du lieu nhe
            $rows = $db->executeQuery($sql, $parameters, $values, $types);
            $objects = array();
            if ($rows != null) {
                while ($row = $rows->fetch()) {
                    $obj = new M_Binh_luan();
                    $obj->setBL_ID($row->BL_ID);
                    $obj->setHH_ID($row->HH_ID);
                    $obj->setTK_ID($row->TK_ID);
                    $obj->setBL_NOIDUNG($row->BL_NOIDUNG);
                    $obj->setBL_NGAYDANG($row->BL_NGAYDANG);
                    $obj->setTK_AVATAR($row->TK_AVATAR);
                    $obj->setTK_USERNAME($row->TK_USERNAME);
                    $objects[] = $obj;
                }
                return $objects;
            }
        }
    }

}
?>

